package 算法.背包;

import java.util.Iterator;

import util.LogUtils;

/**
 * 【背包模型】
 * 		一个只能add和遍历的集合，无需考虑是否有序
 * @Date   2017-09-03
 * @author Administrator
 * @version 
 */
public class Bag<Item> implements Iterable<Item> {
	private static final String TAG = "背包模型";
	private BagNode<Item> first;
	private class BagNode<Item> {
		Item item;
		BagNode<Item> next;
	}
	public void add(Item item) {
        BagNode<Item> oldFirst = first;
        first.item = item;
        first.next = oldFirst;
	}
	@Override
	public Iterator<Item> iterator() {
		return new BagIterator();
	}
	//迭代器
	private class BagIterator implements Iterator<Item> {
		BagNode<Item> node = first;
        @Override
        public boolean hasNext() {
            return node.next != null;
        }
        @Override
        public Item next() {
            Item item = node.item;
            node = node.next;
            return item;
        }
		@Override
		public void remove() {LogUtils.println(TAG, "背包模型不支持remove");}
	}
}
